package edu.lnu.ums.ui.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import edu.lnu.ums.entities.dictionaries.Sex;
import edu.lnu.ums.service.IDictionaryService;
import edu.lnu.ums.ui.TreeBean;

public class SexConverter implements Converter{

    private IDictionaryService dictionaryService;

    private TreeBean treeBean;

    public SexConverter() {
        try {
            dictionaryService = (IDictionaryService) InitialContext.doLookup("UMS/DictionaryService/local");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String name) {
        if ("".equals(name)) {
            return null;
        }
        
        return dictionaryService.getSexByName(treeBean.getSelectedObject(), name);
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
        if (obj == null) {
            return "";
        }
        
        return ((Sex)obj).getName();
    }

    public TreeBean getTreeBean() {
        return treeBean;
    }

    public void setTreeBean(TreeBean treeBean) {
        this.treeBean = treeBean;
    }

}
